package com.bw.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class CookieController {


    /**
     * 浏览器第一次访问服务器
     * @return
     */
    @RequestMapping("/one.do")
    public String one(HttpServletRequest request,HttpServletResponse response){
        request.getParameter("name");
        request.getParameter("password");
        // 创建一个cookie
        Cookie cookie = new Cookie("2106A", "26ren");
        // response 代表响应给客户端的数据  返回给客户端
        response.addCookie(cookie);
        return "success";
    }

    /**
     * 浏览器第二次访问服务器
     * @return
     */
    @RequestMapping("/two.do")
    public String two(HttpServletRequest request){

        String header = request.getHeader("User-Agent");
        System.out.println("浏览器发送过来的请求头内容User-Agent：" + header);
        Cookie[] cookies = request.getCookies();
        System.out.println("浏览器发送过来的cookie" + JSON.toJSONString(cookies));

        return "success";

    }
}
